package com.qf.mybatisExam01.entity;
import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book implements Serializable {
    private Integer id;
    private String name;
    private  String author;
    private  String publish;
    private Integer sort;


    public Book(String name, String author, String publish, Integer sort) {
        this.name = name;
        this.author = author;
        this.publish = publish;
        this.sort = sort;
    }

    /**
     * 将book对象转换成 csv 字符串
     * @return
     */
    public String toCSVString(){
        return this.name+","+this.author+","+this.publish+","+this.sort;
    }
    @Override
    public String toString(){
        return this.name+","+this.author+","+this.publish+","+this.sort;
    }

    /**
     * 随机生成一本书
     * @return
     */
    public static Book randomOneBook(){

        String name = RandomUtil.randomString(20);
        String author = RandomUtil.randomString(10);
        String publish = RandomUtil.randomString(20);
        Integer sort = RandomUtil.randomInt(0,500000);
        Book book = new Book(name, author, publish, sort);

        return book;
    }

    /**
     * 随机生成num数量的书
     * @param num
     * @return
     */
    public static List<Book> randomManyBooks(Integer num){
        List<Book> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(randomOneBook());
        }
        return list;
    }
}
